package cn.zmall.sso.service.impl;

import cn.zmall.common.jedis.JedisClient;
import cn.zmall.manager.dto.DtoUtil;
import cn.zmall.manager.dto.front.Member;
import cn.zmall.manager.mapper.TbMemberMapper;
import cn.zmall.manager.pojo.TbMember;
import cn.zmall.manager.pojo.TbMemberExample;
import cn.zmall.sso.service.LoginService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;

/**
 * @Author BinBini
 * @Date 2020/10/29 10:45
 * @Version 1.0
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private TbMemberMapper tbMemberMapper;
    @Autowired
    private JedisClient jedisClient;
    @Value("1800")
    private Integer SESSION_EXPIRE;

    @Override
    public Member userLogin(String username, String password) {
        TbMemberExample example = new TbMemberExample();
        TbMemberExample.Criteria criteria = example.createCriteria();
        criteria.andStateEqualTo(1);
        criteria.andUsernameEqualTo(username);
        List<TbMember> list = tbMemberMapper.selectByExample(example);
        if( list == null||list.size() == 0 ) {
            Member member = new Member();
            member.setState(0);
            member.setMessage("用户名或密码错误");
            return member;
        }
        TbMember tbMember = list.get(0);
        // MD5加密
        if(!DigestUtils.md5DigestAsHex(password.getBytes()).equals(tbMember.getPassword())) {
            Member member = new Member();
            member.setState(0);
            member.setMessage("用户名或密码错误");
            return member;
        }
        String token = UUID.randomUUID().toString();
        Member member = DtoUtil.TbMemberToMember(tbMember);
        member.setToken(token);
        member.setState(1);
        // 用户信息写入redis, key: "SESSION:token" value:"user"
        jedisClient.set("SESSION:" + token, new Gson().toJson(member));
        jedisClient.expire("SESSION:" + token, SESSION_EXPIRE);
        return member;
    }

    @Override
    public Member wxLogin(String wxId) {
        TbMember tbMember = tbMemberMapper.selectByWxKey(wxId);
        String token = UUID.randomUUID().toString();
        Member member = DtoUtil.TbMemberToMember(tbMember);
        member.setToken(token);
        member.setState(1);
        // 用户信息写入redis, key: "SESSION:token" value:"user"
        jedisClient.set("SESSION:" + token, new Gson().toJson(member));
        jedisClient.expire("SESSION:" + token, SESSION_EXPIRE);
        return member;
    }

    @Override
    public Member getUserByToken(String token) {
        String json = jedisClient.get("SESSION:" + token);
        if(json == null) {
            Member member = new Member();
            member.setState(0);
            member.setMessage("用户登录已过期");
            return member;
        }
        // 重置过期时间
        jedisClient.expire("SESSION:" + token, SESSION_EXPIRE);
        Member member = new Gson().fromJson(json, Member.class);
        return member;
    }

    @Override
    public int logout(String token) {
        jedisClient.del("SESSION:" + token);
        return 1;
    }

    @Override
    public Member getUserInfo(Long userId) {
        TbMember tbMember = tbMemberMapper.selectByPrimaryKey(userId);
        if (tbMember == null){
            return null;
        }
        Member member = DtoUtil.TbMemberToMember(tbMember);
        return member;
    }
}
